package com.tensquare.sms.listener;

import com.tensquare.sms.util.SmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
@RabbitListener(queues = "sms")
public class ListenerSMS {

    @Autowired
    private SmsUtil smsUtil ;

    @Value("aliyun.sms.template_code")
    private String template_code ;

    @Value("aliyun.sms.sign_name")
    private String sign_name ;

    @RabbitHandler
    public void executeSMS(Map<String,String> map) {
        String mobile = map.get("mobile") ;
        String code = map.get("code") ;
        log.info(" sms-mobile : " + mobile);
        log.info(" sms-code : " + code);

        // String mobile,String template_code,String sign_name,String param
        try {
            String param = "{'code:'" + code + "'}";
            smsUtil.sendSms(mobile, template_code, sign_name, param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("短信发送完毕");


    }


}
